import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;

public class BatchOperation extends JFrame implements ActionListener
{
	//Singleton
	private static BatchOperation bo=null;
	
	JLabel lblHeading,lblBatchCode,lblBatchDesc;
	JTextField txtBatchCode,txtBatchDesc;
	JButton  btnCreate,btnExit;
	
	private BatchOperation()
	{
		//Outer Panel
		JPanel screenMain= (JPanel)getContentPane( );
		screenMain.setLayout( new BorderLayout() );		
		screenMain.setBorder(new EmptyBorder(10, 10, 10, 10));
		//
		//Inner Panel
		JPanel screenSub=new JPanel( );
		screenSub.setLayout( new GridLayout( 3,3,8,8 ));		
		//
		
		//Labels
		//Heading
		lblHeading=new JLabel(ProgramUitl.ADD_BATCH_TEXT);
		Font fontHeading=new Font(ProgramUitl.FONT_NAME,
									ProgramUitl.HEADING_FONT_STLYE,
									ProgramUitl.HEADING_FONT_SIZE);
		lblHeading.setFont(fontHeading);
		screenMain.add("North",lblHeading);		
		//
		Font fontLbl=new Font(ProgramUitl.FONT_NAME,
									ProgramUitl.NORMAL_FONT_STLYE,
									ProgramUitl.NORMAL_FONT_SIZE);
		
		lblBatchCode=new JLabel(ProgramUitl.BATCH_CODE_TEXT);						
		lblBatchCode.setFont(fontLbl);
		
		lblBatchDesc=new JLabel(ProgramUitl.BATCH_DESC_TEXT);						
		lblBatchDesc.setFont(fontLbl);		
		//

		//Text Boxes
		//txtStudentNo=new JTextField();
		txtBatchCode=new JTextField();
		txtBatchDesc=new JTextField();		
		//
		
		//Buttons
		btnCreate=new JButton(ProgramUitl.CREATE_TEXT);
		btnExit=new JButton(ProgramUitl.EXIT_TEXT,ProgramUitl.getButtonIcon(ProgramUitl.ICON_TYPE.ERROR));
		
		btnCreate.addActionListener(this);
		btnExit.addActionListener(this);
		//

		//Layout Design
		
		screenSub.add(lblBatchCode);
		screenSub.add(txtBatchCode);
		
		screenSub.add(lblBatchDesc);
		screenSub.add(txtBatchDesc);

		screenSub.add(btnCreate);		
		screenSub.add(btnExit);		
		//
		
		//Inner panel added to Outer Panel 
		screenMain.add("Center",screenSub);
		//
		
		//Frame Settings
		addWindowListener(new WindowListener() {			
			public void windowClosing(WindowEvent e){}
			public void windowOpened(WindowEvent e) 
			{
				MainMenu.getInstance().setVisible(false);
				MainMenu.getInstance().setEnabled(false);
			}			       
			public void windowClosed(WindowEvent e) {}
			public void windowIconified(WindowEvent e) {}
			public void windowDeiconified(WindowEvent e) {}
			public void windowActivated(WindowEvent e) {}
			public void windowDeactivated(WindowEvent e) {}
		});


		
		setSize(ProgramUitl.SMALL_SCREEN_WIDTH,ProgramUitl.SMALL_SCREEN_HEIGHT);
		setLocation(ProgramUitl.SCREEN_LOCATION_TOP,ProgramUitl.SCREEN_LOCATION_LEFT);
		setLocationRelativeTo(null);
		setTitle(ProgramUitl.HEADNING_TEXT);		
        setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);		
		setUndecorated(true);		
		setVisible(true);
		
		validate();
		//		
	}
	
	public static BatchOperation getInstance()
	{
		if(bo==null)
		{	
			bo=new BatchOperation();
		}
		
		return bo;
	}
	//
	
	//Methods
	void exit() 
	{ 		
		int confirmed = JOptionPane.showConfirmDialog(null, 
				ProgramUitl.EXIT_PRORAM_MSG, ProgramUitl.ADD_COURSE_TEXT, 
				JOptionPane.YES_NO_OPTION); 
				
		if (confirmed == JOptionPane.YES_OPTION) 
		{        
				MainMenu.getInstance().setEnabled(true);
				MainMenu.getInstance().setVisible(true);
				bo=null;
				this.dispose();
		} 
	}
	public void clearText()
	{		
		txtBatchCode.setText(null);
		txtBatchDesc.setText(null);	
		
		txtBatchCode.requestFocus();		
	}	

	public boolean isValidInputs()
	{
		String msg="";
		
		//Code
		if(ProgramUitl.IsEmptyString(txtBatchCode.getText().trim()))
		{
			msg+=ProgramUitl.buildMessageItem(txtBatchCode.getText().trim() + ProgramUitl.REQUIRED_FIELD_ERROR);
		}
		else if(ProgramUitl.isPositiveNumeric(txtBatchCode.getText().trim())==false)
		{
			msg+=ProgramUitl.buildMessageItem(lblBatchCode.getText().trim() + ProgramUitl.INVALID_FORMAT_ERROR);
		}
	
		if(ProgramUitl.IsEmptyString(txtBatchDesc.getText().trim()))
		{
			msg+=ProgramUitl.buildMessageItem(lblBatchDesc.getText().trim() + ProgramUitl.REQUIRED_FIELD_ERROR);
		}
		
		if(ProgramUitl.IsEmptyString(msg)!=true)
		{
			JOptionPane.showMessageDialog(null,msg,ProgramUitl.ADD_BATCH_TEXT,JOptionPane.ERROR_MESSAGE);
		}
		

		return ProgramUitl.IsEmptyString(msg);		
			
	}
	
	void createBatch()
	{
		if(isValidInputs())
		{
			if(Confirmation(ProgramUitl.BATCH_CONFIRMATION))
			{
				Batch b=new Batch();
				
				b.setBatchCode(Integer.parseInt(txtBatchCode.getText().trim()));
				b.setBatchDesc(txtBatchDesc.getText().trim());
			
				b.setStatus(ProgramUitl.STATUS.ENABLED);
				
				try
				{
					ProgramUitl.ERROR_TYPE rt=XmlOperations.getInstance().addBatch(b);
					
					switch(rt)
					{
						case ADDED:
							{					
								JOptionPane.showMessageDialog(null,b.getBatchCode() + ProgramUitl.ADDED_SUCCESS_TEXT,ProgramUitl.ADD_BATCH_TEXT,JOptionPane.INFORMATION_MESSAGE);
								clearText();						
							}
							break;
						case ALREADY_EXISTS:
							JOptionPane.showMessageDialog(null,txtBatchCode.getText().trim() + ProgramUitl.ALREADY_EXISTS_TEXT,ProgramUitl.ADD_BATCH_TEXT,JOptionPane.WARNING_MESSAGE);
							break;
						default:
							break;
					}
				}
				catch(Exception ex)
				{
					JOptionPane.showMessageDialog(null,ex.getMessage(),ProgramUitl.ADD_BATCH_TEXT,JOptionPane.ERROR_MESSAGE);	
				}
			}
		}	
		
	}
	boolean Confirmation(String txt)
	{
		int confirmed = JOptionPane.showConfirmDialog(null, 
				txt, ProgramUitl.UPDATE_STUDENT_MARKS_TEXT, 
				JOptionPane.YES_NO_OPTION); 
				
		return confirmed == JOptionPane.YES_OPTION;
	}	
	//
	//Events	
	public void actionPerformed(ActionEvent ae)
	{
		if(ae.getSource().equals(btnExit))
		{
			exit();
		}	
		else if(ae.getSource().equals(btnCreate))
		{			
			createBatch();			
		}		
	}	
	//
}